/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//    Copyright(c) 2000 - 2001 Intel Corporation. All Rights Reserved.
//
//  VSS: 
//     $Workfile: $
//     $Revision: $
//     $Date:     $
//     $Archive:  $
//
//  Description:
//      Intel(R) Integrated Performance Primitives - JPEG Helper file.
//      This file contains table initializing functions for DCT. 
//
//  History:
//     Date         Author        Changes
//     2000/12/22  Jerry Hu       Created (Version 0.2, Winograd algorithm )
//
********************************************************************************/
#include "_ippjpeg.h"


/*
//  f(i, j)=1/(x[i]*x[j]*8);
//
//  where
//  x[n] = 1  if n=0, 4
//  x[n] = 1/(sqrt(2)*cos(n*pi/16))
//
*/

static const int _DCTCoeff[64] =
{

    4096,   2953,   3135,   3483,   4096,   5213,   7568,   14846,  
    2953,   2129,   2260,   2511,   2953,   3759,   5457,   10703,  
    3135,   2260,   2399,   2666,   3135,   3990,   5793,   11363,  
    3483,   2511,   2666,   2962,   3483,   4433,   6436,   12625,  
    4096,   2953,   3135,   3483,   4096,   5213,   7568,   14846,  
    5213,   3759,   3990,   4433,   5213,   6635,   9633,   18895,  
    7568,   5457,   5793,   6436,   7568,   9633,   13985,  27432,  
    14846,  10703,  11363,  12625,  14846,  18895,  27432,  53809
};

/******************************************************************************
// Name:             ippiDCTQuantFwdTableInit_JPEG_16u
// Description:      Merging DCT coefficients into quantization table.
// Input Arguments: 
//   pSrcQuantTable: Identifies source quantization table.
//
// Output Arguments:
//   pDstQuantTable: Identifies destination quantization table.
//                  
// Remarks:      
//                   1. The start address of "pSrcQuantTable" and  " pDstQuantTable"
//                      shall be aligned at 4byte boundary.
//                   2. It is better to do this alignement at 32byte boundary to optimize 
//                      D-Cache accessing.
//                   
// Returns:         
//    IPP_STATUS_OK: Succeeds.
//
******************************************************************************/
IPPFUN(IppStatus, ippiDCTQuantFwdTableInit_JPEG_16u) (const Ipp16u *pSrcQuantTable,
                                                     Ipp16u *pDstQuantTable)
{
    int i;
    const int *pCoeff;
    Ipp16u *pQTCur;

    _IPP_CHECK_ARG(NULL!=pSrcQuantTable);
    _IPP_CHECK_ARG(_IPP_CHECK_ALIGN_4BYTE(pSrcQuantTable));
    _IPP_CHECK_ARG(NULL!=pDstQuantTable);
    _IPP_CHECK_ARG(_IPP_CHECK_ALIGN_4BYTE(pDstQuantTable));

    pCoeff=_DCTCoeff;
    pQTCur=pDstQuantTable;

    for (i=8; i>0; i--) {
        pQTCur[0]  =  (Ipp16u)((pCoeff[0] + (pSrcQuantTable[0]>>1))/pSrcQuantTable[0]);
        pQTCur[8]  =  (Ipp16u)((pCoeff[1] + (pSrcQuantTable[1]>>1))/pSrcQuantTable[1]);
        pQTCur[16] =  (Ipp16u)((pCoeff[2] + (pSrcQuantTable[2]>>1))/pSrcQuantTable[2]);
        pQTCur[24] =  (Ipp16u)((pCoeff[3] + (pSrcQuantTable[3]>>1))/pSrcQuantTable[3]);
        pQTCur[32] =  (Ipp16u)((pCoeff[4] + (pSrcQuantTable[4]>>1))/pSrcQuantTable[4]);
        pQTCur[40] =  (Ipp16u)((pCoeff[5] + (pSrcQuantTable[5]>>1))/pSrcQuantTable[5]);
        pQTCur[48] =  (Ipp16u)((pCoeff[6] + (pSrcQuantTable[6]>>1))/pSrcQuantTable[6]);
        pQTCur[56] =  (Ipp16u)((pCoeff[7] + (pSrcQuantTable[7]>>1))/pSrcQuantTable[7]);
        pCoeff+=8;
        pSrcQuantTable+=8;
        pQTCur++;
    }

    return IPP_STATUS_OK;
}

/******************************************************************************
// Name:             ippiDCTQuantInvTableInit_JPEG_16u32s
// Description:      Merging IDCT coefficients into dequantization table.
// Input Arguments: 
//   pSrcQuantTable: Identifies source dequantization table.
//
// Output Arguments:
//   pDstQuantTable: Identifies destination dequantization table.
//                  
// Remarks:      
//                   1. The start address of "pSrcQuantTable" and  " pDstQuantTable"
//                      shall be aligned at 4byte boundary.
//                   2. It is better to do this alignement at 32byte boundary to optimize 
//                      D-Cache accessing.
//                   
// Returns:         
//    IPP_STATUS_OK: Succeeds.
//
******************************************************************************/
IPPFUN(IppStatus, ippiDCTQuantInvTableInit_JPEG_16u32s) (const Ipp16u *pSrcQuantTable,
                                                         Ipp32s *pDstQuantTable)
{
    int i;
    Ipp32s *pQTCur;
    const int *pCoeff;

    _IPP_CHECK_ARG(NULL!=pSrcQuantTable);
    _IPP_CHECK_ARG(_IPP_CHECK_ALIGN_4BYTE(pSrcQuantTable));
    _IPP_CHECK_ARG(NULL!=pDstQuantTable);
    _IPP_CHECK_ARG(_IPP_CHECK_ALIGN_4BYTE(pDstQuantTable));

    pQTCur=pDstQuantTable; /* scaled by2^12 */
    pCoeff=_DCTCoeff;

    for (i=8; i>0; i--) {
        pQTCur[0] =  (Ipp32s)((pCoeff[0]*pSrcQuantTable[0]+4)>>3);
        pQTCur[1] =  (Ipp32s)((pCoeff[1]*pSrcQuantTable[1]+4)>>3);
        pQTCur[2] =  (Ipp32s)((pCoeff[2]*pSrcQuantTable[2]+4)>>3);
        pQTCur[3] =  (Ipp32s)((pCoeff[3]*pSrcQuantTable[3]+4)>>3);
        pQTCur[4] =  (Ipp32s)((pCoeff[4]*pSrcQuantTable[4]+4)>>3);
        pQTCur[5] =  (Ipp32s)((pCoeff[5]*pSrcQuantTable[5]+4)>>3);
        pQTCur[6] =  (Ipp32s)((pCoeff[6]*pSrcQuantTable[6]+4)>>3);
        pQTCur[7] =  (Ipp32s)((pCoeff[7]*pSrcQuantTable[7]+4)>>3);

        pCoeff+=8;
        pSrcQuantTable+=8;
        pQTCur+=8;
    }

    return IPP_STATUS_OK;
}

/* EOF */
